//
//  LGStatusToolBarView.m
//  微博
//
//  Created by MS on 15-12-14.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGStatusToolBarView.h"
#import "UIView+Frame.h"
@interface LGStatusToolBarView ()
@property (nonatomic,strong)    NSMutableArray * buttonsArray;
@property (nonatomic,strong)    NSMutableArray * dividesArray;
@property (nonatomic,weak) UIButton * reWetBtn;
@property (nonatomic,weak) UIButton * commentBtn;
@property (nonatomic,weak) UIButton * likeBtn;

@end

@implementation LGStatusToolBarView

-(NSMutableArray *)buttonsArray
{
    if (_buttonsArray ==nil) {
        _buttonsArray =[[NSMutableArray alloc] init];
    }
    return _buttonsArray;
}

-(NSMutableArray *)dividesArray
{
    if (_dividesArray==nil) {
        _dividesArray = [NSMutableArray array];
    }
    return _dividesArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setUpAllsubView];
        self.userInteractionEnabled = YES;
        self.image = [UIImage imageNamed:@"timeline_card_bottom_background"];
        
    }
    return self;
}


/**
 *  添加toolBar子控件
 */
-(void)setUpAllsubView
{
    //转发
   UIButton * reWetBtn = [self setupOneBtnWithImage:[UIImage imageNamed:@"timeline_icon_retweet"] andTitle:@"转发"];
    _reWetBtn = reWetBtn;
    
    //评论
    UIButton * commentBtn=[self setupOneBtnWithImage:[UIImage imageNamed:@"timeline_icon_comment"] andTitle:@"评论"];
    _commentBtn = commentBtn;
    //赞
   UIButton * likeBtn = [self setupOneBtnWithImage:[UIImage imageNamed:@"timeline_icon_unlike"] andTitle:@"赞"];
    _likeBtn = likeBtn;
    
    for (int i = 0; i<2; i++) {
         UIImageView * divide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"timeline_card_bottom_line"]];
        [self.dividesArray addObject:divide];
        [self addSubview:divide];
    }
   
    
    
}

-(UIButton *)setupOneBtnWithImage:(UIImage *)image andTitle:(NSString *)title
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [btn setTitle:title  forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateNormal];
    [self.buttonsArray addObject:btn];
    [self addSubview:btn];
    return btn;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger count = self.buttonsArray.count;
    CGFloat w = self.frame.size.width/count;
    CGFloat h = self.frame.size.height;
    CGFloat y =0;
    for (int i = 0; i<count; i++) {
        UIButton * btn = self.buttonsArray[i];
        btn.frame = CGRectMake(w*i, y, w, h);
    }
    int i =1;
    for (UIImageView * divide in self.dividesArray) {
        UIButton *btn =self.buttonsArray[i];
        divide.x = btn.x;
        i++;
    }
}

-(void)setStatus:(LGStatus *)status
{
    _status = status;
    [self setBtnCountWithBtn:self.commentBtn WithNumber:status.comments_count];
    [self setBtnCountWithBtn:self.likeBtn WithNumber:status.attitudes_count];
    [self setBtnCountWithBtn:self.reWetBtn WithNumber:status.reposts_count];
}


-(void)setBtnCountWithBtn:(UIButton *)btn WithNumber:(int)number
{
    NSString * title = nil;
    if (number) {
        if (number>10000) {
            title = [NSString stringWithFormat:@"%f万",number/10000.0];
            title  = [title stringByReplacingOccurrencesOfString:@".0" withString:@""];
        }
        else{
            title = [NSString stringWithFormat:@"%d",number];
        }
        
        [btn setTitle:title forState:UIControlStateNormal];
    }
 
}

@end
